<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%><%@taglib uri="http://java.sun.com/jstl/core"
	prefix="c"%>
<%
	System.out.println("" + request.getHeader("cookie"));
	if (request.getHeader("cookie") == null) {
		response.sendRedirect("");
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style type="text/css">
body {
	margin: 0px;
}

iframe {
	width: 100%;
	border: none;
}
</style>
<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="autoHeight.js"></script>
<script type="text/javascript">
	// location == location.protocol + "//" + location.host + location.pathname + location.search + location.hash
	var prefix = "<c:url value='/p'></c:url>/";

	var host;

	function proxy() {

		var url = document.form1.url.value;

		// TODO https
		if (url.indexOf("http") == 0) {
			alert("please remove the protocol part (http:// or https://) and try again");
			return;
		}

		frame1.location = prefix + url;

		var firstSlash = url.indexOf("/");
		if (firstSlash == -1) {
			host = url + "/";
		} else {
			host = url.substring(0, firstSlash);
		}

		return;
	}

	function modify($tags, attr) {

		$tags.each(function(index, Element) {
			var url = $(this).attr(attr);
			if (url && url.indexOf(prefix) == -1) {
				if (url.indexOf("/") == 0) {
					url = host + url;
					$(this).attr(attr, prefix + url);
				} else if (url.indexOf("http://") == 0) {
					url = url.substring("http://".length);
					if (url.indexOf("/") == -1) {
						url = url + "/";
					}
					$(this).attr(attr, prefix + url);
				}
				//alert($(this).attr("href"));
			}
		});

		return;
	}

	$(function() {
		$("iframe").load(function() {

			modify($(frame1.document).find("script"), "src");
			modify($(frame1.document).find("link"), "href");
			modify($(frame1.document).find("a"), "href");

			return;
		});
	});
</script>
</head>
<body>
<form name="form1" action="javascript:proxy();"><label>url:
<input name="url" value="labs.romeo-foxtrot.com" /></label> <input
	type="submit" /></form>
<hr />
<iframe name="frame1" class="autoHeight" src="about:blank"></iframe>
</body>
</html>